package com.softfly.bigdata.spark;

import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.VoidFunction;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MapUnionDemo {

    public static void main(String[] args) throws InterruptedException {
        SparkConf conf = new SparkConf().setMaster("local").setAppName("wordcount1");
        JavaSparkContext sc = new JavaSparkContext(conf);
        List<Integer> list = new ArrayList<>();
        for (int i = 0 ; i < 10; ++i) {
            list.add(i);
        }
        JavaRDD<Integer> rdd1 = sc.parallelize(list);

        JavaRDD<String> rdd2 = rdd1.flatMap(new FlatMapFunction<Integer, String>() {
            @Override
            public Iterator<String> call(Integer integer) throws Exception {
                List<String> list = new ArrayList<>();
                list.add(String.valueOf(integer));
                list.add("a" + integer);
                return list.iterator();
            }
        });

        rdd2.foreach(x -> System.out.println(x));
//        JavaRDD<Integer> rdd2 = rdd1.filter(new Function<Integer, Boolean>() {
//            @Override
//            public Boolean call(Integer integer) throws Exception {
//                return integer > 12;
//            }
//        });
//        rdd2.foreach(new VoidFunction<Integer>() {
//            @Override
//            public void call(Integer integer) throws Exception {
//                System.out.println("I have: " + integer);
//            }
//        });

        Thread.sleep(Integer.MAX_VALUE);
        //sc.stop();
    }

}
